// 深拷贝与浅拷贝问题
#include <iostream>
#include <string>
using namespace std;
// 如果构造函数中在堆中开辟内存，需要在拷贝构造函数中深拷贝
class Person {
    private:
        int m_age;
        int *m_height;
    public:
        Person() {
            // 无参构造
            cout << "no parameter construct" << endl;
        }
        Person(int age, int height) {
            // 有参构造
            cout << "age parameter construct" << endl;
            m_age = age;
            m_height = new int(height);
        }
        Person(const Person &p) {
            // 拷贝构造
            cout << "copy construct" << endl;
            m_age = p.m_age;
            // m_height = p.m_height; // 浅拷贝
            // 浅拷贝可能导致析构时重复的delete堆区height
            m_height = new int(*p.m_height); // 深拷贝
            // 利用深拷贝解决浅拷贝问题
        }
        ~Person() {
            // 析构函数
            if (m_height != NULL) {
                delete m_height;
                m_height = NULL;
            }
            cout << "destruct" << endl;
        }

};
void test() {
    Person p1(10, 180);
    Person p2(p1);
}
int main(void) {
    test();

    return 0;
}